package com.btpj.shop.controller;

import com.btpj.shop.dto.CategoryDto;
import com.btpj.shop.dto.CommonPage;
import com.btpj.shop.dto.CommonResult;
import com.btpj.shop.dto.param.CategoryParam;
import com.btpj.shop.service.CategoryService;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

@Api(tags = "商品分类相关接口")
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    @ApiOperation(value = "添加商品分类（必须拥有管理员权限）", notes = "level除了1级外，parentId不能为空")
    @PostMapping("/admin/add")
    public CommonResult<Void> addCategory(@RequestBody CategoryParam categoryParam) {
        if (categoryParam.getLevel() == 1 && categoryParam.getParentId() == null) {
            return CommonResult.error("一级商品分类必须传递父类商品编号");
        }
        if (categoryService.addCategory(categoryParam)) {
            return CommonResult.success("添加商品分类成功");
        }
        return CommonResult.error("服务器出错，添加商品分类失败");
    }

    @ApiOperation(value = "获取商品分类分页列表信息", notes = "不分页pageNo和pageSize不传即可")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentId", value = "父级别ID，第一级可不传递或传null", paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "pageNo", value = "第几页", paramType = "query", dataType = "Int"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", paramType = "query", dataType = "Int")
    })
    @GetMapping("/pageList")
    public CommonResult<CommonPage<CategoryDto>> getCategoryList(Long parentId, Integer pageNo, Integer pageSize) {
        if (pageNo != null && pageSize != null) {
            PageHelper.startPage(pageNo, pageSize);
        }
        List<CategoryDto> list = categoryService.getCategoryDtoList(parentId);
        return CommonResult.success(CommonPage.listPage(list));
    }

    @ApiOperation(value = "获取商品分类信息")
    @ApiImplicitParam(name = "id", value = "商品分类ID", paramType = "path", dataType = "Long", required = true)
    @GetMapping("/details/{id}")
    public CommonResult<CategoryDto> getCategoryDto(@PathVariable Long id) {
        if (!categoryService.exists(id)) {
            return CommonResult.error("不存在指定ID为" + id + "的商品分类");
        }

        return CommonResult.success(categoryService.getCategoryDto(id));
    }

    @ApiOperation(value = "修改商品分类信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "商品分类ID", paramType = "path", dataType = "Long", required = true),
            @ApiImplicitParam(name = "name", value = "商品分类名称", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "sort", value = "商品分类排序值（越大越前）", paramType = "query", dataType = "Int", required = true)
    })
    @PutMapping("/update/{id}")
    public CommonResult<Void> updateCategory(@PathVariable Long id,
                                             @RequestParam @Validated @NotBlank String name,
                                             @RequestParam @Validated @NotNull Integer sort) {
        if (name.trim().length() == 0) {
            return CommonResult.error("商品分类名称不能传空串");
        }
        if (!categoryService.exists(id)) {
            return CommonResult.error("不存在指定ID为" + id + "的商品分类");
        }
        if (categoryService.updateCategory(id, name, sort)) {
            return CommonResult.success("修改商品分类信息成功");
        }
        return CommonResult.error("服务器出错，修改商品分类信息失败");
    }

    @ApiOperation(value = "批量删除商品分类信息（id以逗号隔开即可）")
    @ApiImplicitParam(name = "ids", value = "商品分类ID集", paramType = "path", dataType = "Long", required = true, allowMultiple = true)
    @DeleteMapping("/delete/{ids}")
    public CommonResult<Void> deleteCategories(@PathVariable Long[] ids) {
        for (Long id : ids) {
            if (!categoryService.exists(id)) {
                return CommonResult.error("不存在指定ID为" + id + "的商品分类");
            }
        }
        if (categoryService.deleteCategories(ids)) {
            return CommonResult.success("删除商品分类信息成功");
        }
        return CommonResult.error("服务器出错，删除商品分类信息失败");
    }
}
